--
module("UI.Windows.AddUSDTCardWin", package.seeall)

local UIUtility = require("UI.Utility")
local UIWindow = require("UI.UIWindow").UIWindow
local UIWinMgr = require("UI.UIWinMgr").UIWinMgr
local json = require("cjson")
AddUSDTCardWin = UIWindow:new()

AddUSDTCardWin.name = "AddUSDTCardWin"


function AddUSDTCardWin:Init()
	
	local t = self.Container.transform
 
    self.close_btn=UIUtility.GetChildTransform("close_btn", t, true)
	self:BindEvents()


    self.queren_btn = UIUtility.GetChildTransform("queren_btn", t, true)
    self.tibidizhi_input = UIUtility.FindContorl("UIInput", "tibidizhi_input", t)
    self.bank_user_name_input = UIUtility.FindContorl("UIInput", "bank_user_name_input", t)
    self.bank_cardNumber_input = UIUtility.FindContorl("UIInput", "bank_cardNumber_input", t)
    self.bank_passWord_input = UIUtility.FindContorl("UIInput", "bank_passWord_input", t)

    self.type1_toggle = UIUtility.FindContorl("UIToggle", "type1_toggle", t)
    self.type2_toggle = UIUtility.FindContorl("UIToggle", "type2_toggle", t)

    self.tibi_type=""
    self.type1_toggle.onChange:Clear()
    self.type1_toggle.onChange:Add(UIHelper.CreateEventDelegate(function()
        if self.type1_toggle.value then
            self.tibi_type = "TRC20"
        end
    end, self.type1_toggle.gameObject))
    self.type2_toggle.onChange:Clear()
    self.type2_toggle.onChange:Add(UIHelper.CreateEventDelegate(function()
        if self.type2_toggle.value then
            self.tibi_type = "ERC20"
        end
    end, self.type2_toggle.gameObject))

    self.type2_toggle.onChange:Clear()
    self.type2_toggle.onChange:Add(UIHelper.CreateEventDelegate(function()
        if self.type2_toggle.value then
            self.tibi_type = "ERC20"
        end
    end, self.type2_toggle.gameObject))

    self.type1_toggle.value=true
    
    

    self.isAddCard = true
    self.curBankId = 0
    self.Data = {}
    --获取已存储的信息
    require("NetWork.NetHttp").SendGetRequest("/byr/payment/withdraw/info", function(ok, data)
        if ok then
            print("获取用户提现信息" .. stringify(data))
            for i = 1, #data.data do
                if data.data[i].withdrawMethod == 3 then
                    self.isAddCard = false
                    self.curBankId = data.data[i].withdrawId
                    self.Data = data.data[i]
                    self.bank_user_name_input.value = self.Data.accountName
                    self.tibidizhi_input.value = self.Data.usdtAddress
                    self.tibi_type = self.Data.protocol
                    self.type1_toggle.value = self.Data.protocol == "TRC20"
                    self.type2_toggle.value = self.Data.protocol == "ERC20"
                     return
                end
            end
            self.type1_toggle.value=true
        else
            print(stringify(data))
        end
    end)

    UIHelper.BindUIEvnet("Click", function()
        local saveBankJson 
        -- saveBankJson = string.format('{"withdrawId":%s,"withdrawMethod": 1,"accountName":"%s","bankName": "%s","cardNumber": "%s","secondPassword": "%s"}',
        --self.bank_user_name_input.value, self.xzyh_btn_lable.text, self.bank_cardNumber_input.value, self.bank_passWord_input.value)
        if self.isAddCard then
            saveBankJson = string.format(
                '{"withdrawMethod": 3,"accountName":"%s","usdtAddress": "%s","protocol": "%s","secondPassword": "%s"}',
                self.bank_user_name_input.value, self.tibidizhi_input.value,self.tibi_type,self.bank_passWord_input.value)
        else
            saveBankJson = string.format(
                '{"withdrawId":%s,"withdrawMethod": 3,"accountName":"%s","usdtAddress": "%s","protocol": "%s","secondPassword": "%s"}',
                self.curBankId, self.bank_user_name_input.value, self.tibidizhi_input.value,self.tibi_type,self.bank_passWord_input.value)
        end
        require("NetWork.NetHttp").SendPostRequest_token("/byr/payment/withdraw/saveOrUpdate", function(ok, data)
            if ok then
                print("添加银行卡" .. stringify(data))
                UIWinMgr:OpenTextNotice("USDT设置成功")
                local NewGameLoobyWin = require("UI.Windows.NewGameLoobyWin").NewGameLoobyWin
                NewGameLoobyWin.user_data_zfbinfo = self.Data
                NewGameLoobyWin.secondPassword = self.bank_passWord_input.value
                self:Close(1)
            else
           
                UIWinMgr:OpenTextNotice(data.msg)
            end
        end, saveBankJson)
    end, self.queren_btn.gameObject)
end

function AddUSDTCardWin:OnShow()
	
end

function AddUSDTCardWin:OnClose()

end


function AddUSDTCardWin:BindEvents()
    UIHelper.BindUIEvnet("Click", function()
    self:Close(1)
end, self.close_btn.gameObject)

end

